/**
 * 
 */
package de.oszimtcc.framework;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

import android.app.Dialog;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.ads.AdRequest;
import com.google.ads.AdView;

import de.oszimtcc.timetable.R;

/**
 * @author Kooki
 *
 */
public class BaseScreen implements OnClickListener
{
	public BaseActivity activity;
	public Dialog waitDialog;
	protected Class<? extends BaseScreen> lastScreen;
	protected Map<View, Class<? extends BaseScreen>> screenBindings = new HashMap<View, Class<? extends BaseScreen>>();
	int layoutResourceId;
	private AdView adView;
	
	public BaseScreen(BaseActivity pBaseActivity)
	{
		activity = pBaseActivity;
		activity.setContentView(R.layout.parent_layout);
		
        adView = (AdView)activity.findViewById(R.id.blockAdView);
        AdRequest request = new AdRequest();
        request.addTestDevice("999C08DC57BB129C37F0C9938661E18A");
        adView.loadAd(request);;

	}
	
	protected void Setup(int _layoutResourceId) 
	{ 
		layoutResourceId = _layoutResourceId;
		//activity.setContentView(layoutResourceId);
	    RelativeLayout holder = (RelativeLayout) activity.findViewById(R.id.parentViewHolder);
	    holder.removeAllViews();
	    LayoutInflater layoutInflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);    
	    layoutInflater.inflate(layoutResourceId, holder, true);
		activity.screen = this;
	}
	
	
	public boolean Back() 
	{
		if(lastScreen != null) 
		{
			InstantiateScreen(this.activity,lastScreen.getName());
			lastScreen = null;
			return true;
		}
		return false;
	}
	
	@Override
	public void onClick(View v) 
	{
		Class<? extends BaseScreen> c = screenBindings.get(v);
		if(c!=null) 
		{
			InstantiateScreen(c);
		}
	}
	
	@SuppressWarnings("unchecked")
	public static BaseScreen InstantiateScreen(BaseActivity pBaseActivity, String pName) 
	{
		try 
		{
			Class<?> screenImpl = Class.forName(pName);
		
			Constructor<? extends BaseScreen> constructor = (Constructor<? extends BaseScreen>)screenImpl.getDeclaredConstructors()[0];
			if(constructor == null) 
			{
				return null;
			}
		
			return constructor.newInstance(pBaseActivity);
			
		} 
		catch (IllegalArgumentException e) 
		{
			e.printStackTrace();
		} 
		catch (InstantiationException e) 
		{
			e.printStackTrace();
		} 
		catch (IllegalAccessException e)
		{
			e.printStackTrace();
		} 
		catch (InvocationTargetException e) 
		{
			e.printStackTrace();
		} 
		catch (ClassNotFoundException e) 
		{
			e.printStackTrace();
		}		
		return null;
	}
	
	protected BaseScreen InstantiateScreen(Class<? extends BaseScreen> c, Class<? extends BaseScreen> lastScreen) 
	{
		Constructor<? extends BaseScreen> constructor = null;
		
		constructor = findConstructor(c, lastScreen, constructor);
		if(constructor == null) 
		{
			return null;
		}
		if(activity.screen!=null) 
		{
			//activity.screen.onVanish();
		}
		try 
		{
			Logger.Debug("Screen","instantiating " + c.getName());
			this.lastScreen = this.getClass();
			if(lastScreen == null) 
			{
				activity.screen    = constructor.newInstance(activity);
			} 
			else 
			{
				activity.screen    = constructor.newInstance(activity, lastScreen);
			}
		} 
		catch (IllegalArgumentException e) 
		{
			e.printStackTrace();
		} 
		catch (InstantiationException e) 
		{
			e.printStackTrace();
		} 
		catch (IllegalAccessException e) 
		{
			e.printStackTrace();
		} 
		catch (InvocationTargetException e) 
		{
			e.printStackTrace();
		}
		
		return activity.screen;
	}
	
	protected BaseScreen InstantiateScreen(Class<? extends BaseScreen> c) 
	{
		return InstantiateScreen(c, null);
	}
	
	private static Constructor<? extends BaseScreen> findConstructor(
			Class<? extends BaseScreen> c, 
			Class<? extends BaseScreen> lastScreen,
			Constructor<? extends BaseScreen> constructor) 
	{
		for(Constructor<?> con : c.getDeclaredConstructors()) 
		{
			if(con.getGenericParameterTypes().length == 1 && lastScreen == null) 
			{
				constructor = (Constructor<? extends BaseScreen>)con;
				break;
			} 
			else if(con.getGenericParameterTypes().length == 2 && lastScreen != null) 
			{
				constructor = (Constructor<? extends BaseScreen>)con;
				break;
			}
		}
		return constructor;
	}
	
	protected void bindClickToScreen(int resId, Class<? extends BaseScreen> class1) 
	{
		View v = activity.findViewById(resId);
		if(v != null) 
		{
			screenBindings.put(v, class1);
			v.setOnClickListener(this);
		}
		else
		{
			Logger.Debug("cannot bind not existing view to screen: " + resId);
		}
	}
	
	protected void bindClickToScreen(View view, Class<? extends BaseScreen> class1) 
	{
		screenBindings.put(view, class1);
		view.setOnClickListener(this);
	}
	
	public void StartWaitDialog()
	{
		if(waitDialog!=null) 
		{ 
			RemoveWaitDialog(); 
		}
		waitDialog = new Dialog(this.activity);
		waitDialog.setContentView(R.layout.dlg_waitforserver);
		waitDialog.setTitle("Bitte warten...");
		waitDialog.show();
	}
	
	public synchronized void RemoveWaitDialog() 
	{
		if(waitDialog!=null) 
		{
			waitDialog.dismiss();
		}
		waitDialog=null;
	}
	
	public void DrawError(String pInfoText)
	{
		RemoveWaitDialog();
		activity.setContentView(R.layout.error);
		TextView info = (TextView)activity.findViewById(R.id.errorInfoText);
		info.setText(pInfoText);
	}
	
	public void SetLastScreen(Class<? extends BaseScreen> pLastScreen) 
	{
		//if(this.lastScreen.getSimpleName() != pLastScreen.getSimpleName())
			this.lastScreen = pLastScreen;
	}
	
	public Class<? extends BaseScreen> GetLastScreen()
	{
		return this.lastScreen;
	}
}
